<template>
  <el-form ref="editForm" :model="formItem" :rules="rules" label-width="100px" size="mini">
    <el-row>
      <!-- <el-col :span="8">
        <el-form-item label="任务编号">
          <el-input v-model="formItem.code" disabled />
        </el-form-item>
      </el-col> -->
      <el-col :span="24">
        <el-form-item label="附件">
          <el-upload
            class="upload-demo"
            :headers="headers"
            :action="importUrl"
            :file-list="attachment"
            :before-upload="handleBeforeUpLoad"
            :on-success="handleSuccess"
            :on-error="handleError"
            :on-remove="handleRemove"
            :show-file-list="true"
          >
            <el-button size="small" type="primary" icon="el-icon-upload2">上传</el-button>
          </el-upload>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="附件列表">
          <!-- <el-button v-for="tag in attachment" :key="tag.name" size="mini" type="text" @click.native="clickTag(tag)">{{ tag.name }}</el-button> -->
          <el-tag v-for="tag in attachment" :key="tag.name" closable size="mini" type="info" @click="clickTag(tag)" @close="removeTag(tag)">{{ tag.name }}</el-tag>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import { deleteAttachmentByFsId, updateTaskAttachment, importUrl, exportFile } from '@neu/dcom-ui/api/hnInspectionTaskHealth'
import { getToken } from '@neu/biz-core/utils/auth'
import { clone } from '@neu/biz-core/utils/utils'
export default {
  name: 'HealthUpload',
  props: {
    attach: {
      required: true,
      type: Array
    },
    id: {
      required: true,
      type: Number
    }
  },
  data() {
    return {
      formItem: {},
      rules: {},
      importUrl: importUrl,
      headers: {
        'Access-Control-Allow-Origin': '*',
        'Authentication': getToken()
      },
      attachment: clone(this.attach)
    }
  },
  methods: {
    handleBeforeUpLoad(file) {
    },
    handleSuccess(response, file, fileList) {
      if (response.status === 'failure') {
        this.$message({
          showClose: true,
          message: response.data,
          type: 'error'
        })
      } else {
        this.$message({
          showClose: true,
          message: '文件上传成功',
          type: 'success'
        })
        const obj = {
          fsId: response.data.fsId,
          name: response.data.name
        }
        console.log(obj)
        const fsIdArr = this.attachment.map(t => t.fsId)
        if (!fsIdArr.includes(obj.fsId)) {
          this.attachment.push(obj)
        } else {
          fileList.pop()
        }
      }
    },
    handleError(err, file, fileList) {
      console.log(err)
    },
    handleRemove(file, fileList) {
      this.attachment = this.attachment.filter(t => {
        return t.fsId !== file.fsId
      })
      if (this.id !== -1) {
        return
      }
      deleteAttachmentByFsId(file.fsId, this.id).then(res => {
        // if (res.status === 'failure') {
        //   this.$message({
        //     showClose: true,
        //     message: res.data,
        //     type: 'error'
        //   })
        // } else {
        //   this.$message({
        //     showClose: true,
        //     message: '删除成功',
        //     type: 'success'
        //   })
        // }
      })
    },
    save() {
    //   const updated = this.id !== -1
      return new Promise(resolve => {
        // this.$refs.editForm.validate((valid) => {
        //   if (updated) {
        debugger
        resolve(updateTaskAttachment(this.id, this.attachment))
        //   }
        // })
      })
    },
    clickTag(tag) {
      exportFile(tag.fsId).then(res => {
        const content = res
        const blob = new Blob([content])
        const fileName = tag.name
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    },
    removeTag(item) {
      this.attachment = this.attachment.filter(t => {
        return t.fsId !== item.fsId
      })
      deleteAttachmentByFsId(item.fsId, this.id).then(res => {

      })
    }
  }
}
</script>
